﻿using System;
using System.Collections;
using System.Collections.Generic;
using IDAL;
using Model;

namespace BLL
{
    /// <summary>
    /// 用户权限关系Service
    /// </summary>
    public class UserActionService
    {
        private static readonly IUserActionDAL dal = DALFactory.DataAccess.createUserActionDAL();

        //增加权限
        public Hashtable addUserAction(int userId, int actionId)
        {
            Hashtable ht = new Hashtable();
            UserService us = new UserService();
            User user = us.getUserById(userId);
            if (user == null)
            {
                ht.Add("result", "0");
                ht.Add("message", "用户不存在");
                return ht;
            }
            if (user.status != 2)
            {
                ht.Add("result", "0");
                ht.Add("message", "用户不是管理员");
                return ht;
            }
            ActionService actionService = new ActionService();
            Model.Action action = actionService.getActionById(actionId);
            if (action == null)
            {
                ht.Add("result", "0");
                ht.Add("message", "权限不存在");
                return ht;
            }
            UserAction userAction = new UserAction()
            {
                user = user,
                actionId = actionId
            };
            try
            {
                dal.saveUserAction(userAction);
            }
            catch (Exception e)
            {
                ht.Add("result", "0");
                ht.Add("message", e.Message);
                return ht;
            }
            ht.Add("result", "1");
            ht.Add("message", "权限增加成功");
            return ht;
        }

        //删除权限
        public Hashtable delUserAction(int userId, int actionId)
        {
            Hashtable ht = new Hashtable();
            UserService us = new UserService();
            User user = us.getUserById(userId);
            if (user == null)
            {
                ht.Add("result", "0");
                ht.Add("message", "用户不存在");
                return ht;
            }
            if (user.status != 2)
            {
                ht.Add("result", "0");
                ht.Add("message", "用户不是管理员");
                return ht;
            }
            UserAction userAction = dal.getUserAction(user, actionId);
            if (userAction == null)
            {
                ht.Add("result", "0");
                ht.Add("message", "用户没有该权限");
                return ht;
            }
            try
            {
                dal.delUserAction(userAction);
            }
            catch (Exception e)
            {
                ht.Add("result", "0");
                ht.Add("message", e.Message);
                return ht;
            }
            ht.Add("result", "1");
            ht.Add("message", "权限删除成功");
            return ht;
        }

        //用户的权限列表
        public IList<UserAction> getUserAction(int userId)
        {
            UserService us = new UserService();
            User u = us.getUserById(userId);
            if (u == null)
                return null;
            return dal.getUserAction(u);
        }

        //根据user和actionId得到权限
        public UserAction getUserAction(User user, int actionId)
        {
            return dal.getUserAction(user, actionId);
        }

        //修改权限
        public Hashtable modifyUserAction(int userId, int action1, int action2, int action3)
        {
            Hashtable ht = new Hashtable();
            UserService us = new UserService();
            ActionService actionService = new ActionService();

            User user = us.getUserById(userId);

            if (user == null)
            {
                ht.Add("result", "0");
                ht.Add("message", "用户不存在");
                return ht;
            }
            if (action1 == 0)
            {
                if (getUserAction(user, 1) != null) //删除该权限 
                {
                    delUserAction(userId, 1);
                }
            }
            else
            {
                if (getUserAction(user, 1) == null) //增加该权限 
                {
                    addUserAction(userId, 1);
                }
            }

            if (action2 == 0)
            {
                if (getUserAction(user, 2) != null) //删除该权限 
                {
                    delUserAction(userId, 2);
                }
            }
            else
            {
                if (getUserAction(user, 2) == null) //增加该权限 
                {
                    addUserAction(userId, 2);
                }
            }

            if (action3 == 0)
            {
                if (getUserAction(user, 3) != null) //删除该权限 
                {
                    delUserAction(userId, 3);
                }
            }
            else
            {
                if (getUserAction(user, 3) == null) //增加该权限 
                {
                    addUserAction(userId, 3);
                }
            }
            ht.Add("result", "1");
            ht.Add("message", "权限修改成功");
            return ht;
        }

        //删除用户所有权限
        public Hashtable deleteUserAllAction(int userId)
        {
            Hashtable ht = new Hashtable();
            UserService us = new UserService();
            User user = us.getUserById(userId);
            if (user == null)
            {
                ht.Add("result", "0");
                ht.Add("message", "用户不存在");
                return ht;
            }
            try
            {
                dal.deleteUserAction(user);
            }
            catch (Exception e)
            {
                ht.Add("result", "0");
                ht.Add("message", e.Message);
                return ht;
            }
            ht.Add("result", "1");
            ht.Add("message", "删除成功");
            return ht;

        }

    }
}
